package com.gzhryc.shared_device.oem.h5.agent.controllers.api.v1.referrer;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.NumberTools;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentReferrerWallet;
import com.gzhryc.shared_device.oem.code.agent.services.AgentReferrerWalletService;
import com.gzhryc.shared_device.oem.code.user.ERoleType;
import com.gzhryc.shared_device.oem.code.user.dao.db.User;
import com.gzhryc.shared_device.oem.code.user.services.UserService;
import com.gzhryc.shared_device.oem.h5.agent.ClientAPICodeConstants;
import com.gzhryc.shared_device.oem.h5.agent.LoginUser;
import com.gzhryc.shared_device.oem.h5.agent.WebConstants;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.referrer.AgentReferrerWalletResponse;

public class AgentReferrerWalletController {

	@Route(value = "/agent/api/v1/referrer_user/wallet/List.data")
	public AgentReferrerWalletResponse list(HttpServletRequest request) {
		AgentReferrerWalletResponse apiResp = new AgentReferrerWalletResponse(ClientAPICodeConstants.NULL_DATA,
				"暂无钱包信息");
		LoginUser loginUser = WebUtils.getAttribute(request, WebConstants.ATTR_LOGIN_USER, LoginUser.class);
		if (loginUser != null && ERoleType.Referrer.index().equals(loginUser.getRoleType())) {
			List<AgentReferrerWallet> agentReferrerWalletList = AgentReferrerWalletService.self()
					.findJoinByReferrerUserId(loginUser.getId());

			if (agentReferrerWalletList != null && agentReferrerWalletList.size() > 0) {
				apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
				apiResp.getBody().setItems(new ArrayList<>());

				List<Long> userIds = new ArrayList<>();
				for (AgentReferrerWallet wallet : agentReferrerWalletList) {
					userIds.add(wallet.getUserId());
				}

				List<User> userList = UserService.self().findByUserIds(userIds);

				Long money = 0L;
				Long withdrawnMoney = 0L; // 已提现金额
				for (AgentReferrerWallet wallet : agentReferrerWalletList) {
					money = money + wallet.getMoney();
					withdrawnMoney = withdrawnMoney + wallet.getWithdrawnMoney();

					AgentReferrerWalletResponse.Item item = null;
					for (AgentReferrerWalletResponse.Item temp : apiResp.getBody().getItems()) {
						if (temp.getUserId().equals(wallet.getUserId())) {
							item = temp;
							break;
						}
					}

					if (item != null) {
						item.setMoney(item.getMoney() + wallet.getMoney());
						item.setWithdrawnMoney(item.getWithdrawnMoney() + wallet.getWithdrawnMoney());
					} else {
						item = new AgentReferrerWalletResponse.Item();
						item.setUserId(wallet.getUserId());
						item.setMoney(wallet.getMoney());
						item.setWithdrawnMoney(wallet.getWithdrawnMoney());

						if (userList != null && userList.size() > 0) {
							for (User user : userList) {
								if (user.getId().equals(item.getUserId())) {
									item.setUserName(user.getName());
									item.setUserPhone(user.getPhone());
									break;
								}
							}
						}
					}
					apiResp.getBody().getItems().add(item);
				}
				
				for(AgentReferrerWalletResponse.Item item : apiResp.getBody().getItems()) {
					item.setMoneyStr(NumberTools.getMoneyStr(NumberTools.changeMoneyLi(item.getMoney())));
					item.setWithdrawnMoneyStr(NumberTools.getMoneyStr(NumberTools.changeMoneyLi(item.getWithdrawnMoney())));
				}

				apiResp.getBody().setMoney(NumberTools.getMoneyStr(NumberTools.changeMoneyLi(money)));
				apiResp.getBody().setWithdrawnMoney(NumberTools.getMoneyStr(NumberTools.changeMoneyLi(withdrawnMoney)));
			}
		}
		return apiResp;
	}
}
